//store下的每个文件都是一个store的实例，不需要手动创建
//只需要暴露store的属性（固定写法）
export const state = () => ({
    userInfo: {
        token: '',
        user: {}
    }
})
export const mutations = {
    //设置用户数据
    setUserInfo(state, data) {
        state.userInfo = data
    },
    //清空用户数据
    clearUserInfo(state) {
        state.userInfo = {
            token: '',
            user: {}
        }
    }
}
export const actions = {
    //登录
    login({ commit }, data) {
        return this.$axios({
            url: '/accounts/login',
            method: 'Post',
            data
        }).then(res => {
            commit('setUserInfo', res.data)
            Promise.resolve()
        })
    },
    //注册
    register({ commit }, props) {
        return this.$axios({
            url: '/accounts/register',
            method: 'Post',
            data: props
        }).then(res => {
            commit('setUserInfo', res.data)
        })
    }
}